package code.c01_introduction.homework;

import java.util.Scanner;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/9 21:20
 * @package code.c01_introduction.homework
 * @description 编写一个程序，求解二元一次方程 ax^2 + bx + c = 0 的根
 */

public class SolveEquations {
    public static void main(String[] args) {
        // 用户输入 a b c 组成一个 一元二次方程
        Scanner scanner = new Scanner(System.in);

        System.out.println("请分别输入 a b c 的值（以空格分隔）。");

        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();

        System.out.println(String.format("您输入的方程式是：%dx^2 + %dx + %d = 0", a, b, c));

        // 判断根的个数
        int d = (b * b) - 4 * a * c;
        System.out.println(d);
        double x1, x2;
        if (d > 0) {
            // 使用通式求解
            x1 = ((-1 * b) + Math.sqrt(d)) / (2 * a);
            x2 = ((-1 * b) - Math.sqrt(d)) / (2 * a);
            System.out.println(String.format("x1 = %f \n x2 = %f", x1, x2));
        } else if (d == 0) {
            x1 = ((-1 * b) + Math.sqrt(d)) / (2 * a);
            System.out.println(String.format("x1 = %f", x1));
        } else {
            System.out.println("无实数解，有两个共轭复根");
        }
    }
}
